package com.iscale.util;

import org.apache.commons.lang3.ObjectUtils;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @Author ouyangan
 * @Date
 * @Description 价格计算
 */
public class PriceUtil {


    /**
     * @throws Exception
     * @Author ouyangan
     * @Date 2017-2-18 22:59:17
     * @Description 计算按天的单价
     */
    public static BigDecimal countUnitPriceToDayType(BigDecimal unitPrice, String unitPriceType) throws Exception {
        System.out.println("unitPrice = [" + unitPrice + "], unitPriceType = [" + unitPriceType + "]");
        boolean b = ObjectUtils.allNotNull(unitPrice, unitPriceType);
        if (!b) {
            throw new Exception("参数不能为空");
        }
        if (Objects.equals(unitPriceType, UnitPriceTypeEnum.year.getType())) {
            return unitPrice.divide(new BigDecimal(360), 2);
        } else if (Objects.equals(unitPriceType, UnitPriceTypeEnum.month.getType())) {
            return unitPrice.divide(new BigDecimal(30), 2);
        } else if (Objects.equals(unitPriceType, UnitPriceTypeEnum.day.getType())) {
            return unitPrice;
        } else {
            throw new Exception("单位类型异常");
        }
    }

    /**
     * @throws Exception
     * @Author ouyangan
     * @Date
     * @Description 计算总价
     */
    public static BigDecimal countTotalPrice(BigDecimal unitPrice, String unitPriceType, int days) throws Exception {
        BigDecimal bigDecimal = countUnitPriceToDayType(unitPrice, unitPriceType);
        return bigDecimal.multiply(new BigDecimal(days));
    }
}
